package controller

import (
	"article_vote/logic"
	"github.com/gin-gonic/gin"
	"strconv"
)

type ArticleController struct {}

func (ctrl *ArticleController) Add(ctx *gin.Context) {
	title := ctx.PostForm("title")
	link := ctx.PostForm("link")
	authorId := ctx.PostForm("authorId")

	model := logic.ArticleLogic{}
	err := model.Add(title, link, authorId)
	if err != nil {
		ctx.JSON(200, gin.H{"msg":err})
	}
	ctx.JSON(200, gin.H{"msg":"success"})
}

func (ctrl *ArticleController) Vote(ctx *gin.Context) {
	articleId := ctx.PostForm("articleId")
	userId := ctx.PostForm("userId")

	model := logic.ArticleLogic{}
	err := model.Vote(articleId, userId)
	if err != nil {
		ctx.JSON(200, gin.H{"msg":err.Error()})
		return
	}
	ctx.JSON(200, gin.H{"msg":"success"})
}

func (ctrl *ArticleController) List(ctx *gin.Context) {
	orderType := ctx.Query("orderType")
	offset, _ := strconv.ParseInt(ctx.Query("offset"), 10, 64)
	limit, _ := strconv.ParseInt(ctx.Query("limit"), 10, 64)

	model := logic.ArticleLogic{}
	data, _ := model.List(orderType, offset, limit)
	ctx.JSON(200, gin.H{"msg":"success", "data":data})
}


func (ctrl *ArticleController) GroupList(ctx *gin.Context) {
	orderType := ctx.Query("orderType")
	offset, _ := strconv.ParseInt(ctx.Query("offset"), 10, 64)
	limit, _ := strconv.ParseInt(ctx.Query("limit"), 10, 64)
	group := ctx.Query("group")

	model := logic.ArticleLogic{}
	data, _ := model.GetListByGroup(orderType, offset, limit, group)
	ctx.JSON(200, gin.H{"msg":"success", "data":data})
}
